<template>
  <div class="box">
    <div v-if="name == 'add'">
      <router-view></router-view>
    </div>
    <div v-else>
      <el-input
        placeholder="请输入内容"
        v-model="input3"
        class="input-with-select"
        style="width: 400px"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="search"
        ></el-button>
      </el-input>
      <el-button type="primary" style="margin-left: 20px" @click="add"
        >添加商品</el-button
      >
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%; margin-top: 20px" border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatDate1 }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit"></el-button>
            <el-button type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align: center"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pagenum"
        :page-sizes="[1, 3, 5, 8]"
        :page-size="pageInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  data() {
    return {
      total: 10, //所有商品的数量
      input3: "", //搜索关键字
      pageInfo: {
        //请求商品列表参数
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      tableData: [], //表格数据
      name: "", //获取当前路由的名称
    };
  },
  watch: {
    // 搜索框内容为空重新请求
    input3(val) {
      if (!val) {
        this.pageInfo.pagenum = 1;
        this.pageInfo.pagesize = 5;
        this.getGoodList();
      }
    },
    $route: {
      deep: true,
      immediate: true,
      handler(val) {
        this.name = val.name;
      },
    },
  },
  created() {
    this.getGoodList();
  },
  mounted() {},
  methods: {
    // 封装商品列表请求
    getGoodList() {
      this.$store.dispatch("getGoods", this.pageInfo).then((res) => {
        if (res.meta.status == 200) {
          this.tableData = res.data.goods;
          this.total = res.data.total;
          this.$message.success(res.meta.msg);
        }
      });
    },
    // 每页数据数量
    handleSizeChange(val) {
      this.pageInfo.pagesize = val;
      this.getGoodList();
    },
    // 当前页
    handleCurrentChange(val) {
      this.pageInfo.pagenum = val;
      this.getGoodList();
    },
    // 搜索
    search() {
      this.pageInfo.query = this.input3;
      this.getGoodList();
    },
    // 添加
    add() {
      this.$router.push("/goods/add");
    },
  },
};
</script>

<style scoped lang='scss'>
</style>
